package com.example.administrator.testc.DBs;/**
 * Created by Administrator on 2016/5/25.
 */

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.administrator.testc.mApp;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class DB extends SQLiteOpenHelper {

    private static DB Instance;
    private static final int version = 1;
    public final static String DataName = "cache.db";

    private final String sql_CreatWeb = " ";
//            "create table " + WebBase.TABEL_NAME + " ("
//                    + WebBase.FILE_ID + " TEXT PRIMARY KEY,"
//                    + WebBase.FILE_NAME + " TEXT,"
//                    + WebBase.FILE_URL + " TEXT,"
//                    + WebBase.FILE_DATE + " TEXT,"
//                    + WebBase.FILE_TYPE + " TEXT"
//                    + ");";

    private String getCreatFile(){
        StringBuffer sql_CreatFile = new StringBuffer();
        sql_CreatFile.append("create table " + AdvertisingBean.EAdvertising.TABEL_NAME.getValue() + " (");
        for (AdvertisingBean.EAdvertising e : AdvertisingBean.EAdvertising.values()){
            if(e.name().equals(AdvertisingBean.EAdvertising.TABEL_NAME.name()))
                continue;
            if(e.name().equals("id"))
                sql_CreatFile.append(e.name() + " TEXT PRIMARY KEY,");
            else
                sql_CreatFile.append(e.name() + " TEXT,");
        }
        sql_CreatFile.deleteCharAt(sql_CreatFile.length() - 1);
        sql_CreatFile.append(");");
        Log.i("TAGSS-db", "sql_CreatFile : " + sql_CreatFile.toString());
        return sql_CreatFile.toString();
    }

    private final String sql_Del = "delete database";

    public static DB getInstance() {
        if(Instance == null)
            Instance = new DB(mApp.getContext());
        return Instance;
    }

    public DB(Context context) {
        super(context, DataName, null, version);
    }

    public DB(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        db.execSQL(sql_CreatWeb);
        db.execSQL(getCreatFile());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void closeDB() {
        if (Instance != null) {
            try {
                SQLiteDatabase db = Instance.getWritableDatabase();
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Instance = null;
        }
    }
}
